package TCPClient;

import java.io.IOException;

import messangerTypes.User;

public class TCPClientUI extends javax.swing.JFrame implements Runnable
{
	private static final long serialVersionUID = -9136802203092610913L;
	private TCPClient client;
	private User destination;

	/**
	 * Creates new form ClientUI
	 */
	public TCPClientUI(TCPClient client, User destination)
	{
		this.client = client;
		this.destination = destination;
		initComponents();
		//setLookandFeel();
	}


	public void write (String message)
	{
		this.converationArea.append(this.destination.getNameFirst()+" "+
									this.destination.getNameLast()+": "+message+"\n");
	}
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	
	// This is a chunk of god awful generated code from Netbeans. Don't touch because if it stops working
	//it will be an absolute nightmare to figure out! In fact collapse in the editor and pretend the 
	//implementation is magic. soon i am going to forget that i ever saw this terrible terrible thing.
	private void initComponents()
	{

		jScrollPane1 = new javax.swing.JScrollPane();
		converationArea = new javax.swing.JTextArea();
		jScrollPane2 = new javax.swing.JScrollPane();
		messageEntry = new javax.swing.JTextArea();
		endConversation = new javax.swing.JButton();
		sendButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		addWindowListener(new java.awt.event.WindowAdapter() 
		{
			public void windowClosing(java.awt.event.WindowEvent evt)
			{
				formWindowClosing(evt);
			}
		}
		);

		converationArea.setEditable(false);
		converationArea.setColumns(20);
		converationArea.setLineWrap(true);
		converationArea.setRows(5);
		jScrollPane1.setViewportView(converationArea);

		messageEntry.setColumns(20);
		messageEntry.setLineWrap(true);
		messageEntry.setRows(5);
		messageEntry.setText("");
		jScrollPane2.setViewportView(messageEntry);

		endConversation.setText("End Conversation");
		endConversation.addMouseListener(new java.awt.event.MouseAdapter() 
		{
			public void mouseClicked(java.awt.event.MouseEvent evt)
			{
				endConversationKeyReleased(evt);
			}
		}
		);

		sendButton.setText("Send");
		sendButton.addMouseListener(new java.awt.event.MouseAdapter() 
		{
			public void mouseClicked(java.awt.event.MouseEvent evt)
			{
				sendButtonMouseClicked(evt);
			}
		}
		);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1)
				.addComponent(jScrollPane2)
				.addGroup(layout.createSequentialGroup().addComponent(endConversation)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
				.addComponent(sendButton)))
				.addContainerGap()));
		
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jScrollPane1,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							401,
							javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jScrollPane2,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							100,
							javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(layout.createParallelGroup(
							javax.swing.GroupLayout.Alignment.BASELINE)
							.addComponent(endConversation)
							.addComponent(sendButton))
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
							Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	public void formWindowClosing(java.awt.event.WindowEvent evt)
	{
		this.client.removeClientGUI(destination);
	}

	private void endConversationKeyReleased(java.awt.event.MouseEvent evt)
	{
		System.out.println("end convo");
		this.client.removeClientGUI(destination);
		this.dispose();
	}

	private void sendButtonMouseClicked(java.awt.event.MouseEvent evt)
	{
		String message = messageEntry.getText();
		converationArea.append("me: "+message+"\n");
		messageEntry.setText("");
		try
		{
			this.client.writeMessage(message, destination);
		} catch (IOException e)
		{
			converationArea.append("Message Delivery Failed!\n");
		}
	}

	
	public void setLookandFeel()
	{
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try
		{
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels())
			{
				if ("Nimbus".equals(info.getName()))
				{
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} 
		catch (ClassNotFoundException ex)
		{
			java.util.logging.Logger.getLogger(TCPClientUI.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} 
		catch (InstantiationException ex)
		{
			java.util.logging.Logger.getLogger(TCPClientUI.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} 
		catch (IllegalAccessException ex)
		{
			java.util.logging.Logger.getLogger(TCPClientUI.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} 
		catch (javax.swing.UnsupportedLookAndFeelException ex)
		{
			java.util.logging.Logger.getLogger(TCPClientUI.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}

	}
	

	// Variables declaration - do not modify
	private javax.swing.JTextArea converationArea;
	private javax.swing.JButton endConversation;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea messageEntry;
	private javax.swing.JButton sendButton;
	// End of variables declaration


	@Override
	public void run()
	{
		// TODO Auto-generated method stub
		
	}

}
